﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSDL_NC_07.BUS;
using CSDL_NC_07.DAO;
using System.Security.Cryptography;

namespace CSDL_NC_07.GUI.other
{
    public partial class MHDangNhap : UserControl
    {
        public BACSI bacSi {get;set;}
        public THUNGAN thuNgan {get;set;}

        public MHDangNhap()
        {
            InitializeComponent();
        }

        private void MHDangNhap_Load(object sender, EventArgs e)
        {
            txtTenDangNhap.Focus();
            cbbLoaiTaiKhoan.SelectedIndex = 0;
        }

        #region btnDangNhap_Click
        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (cbbLoaiTaiKhoan.SelectedIndex == 0)
            {
                //đăng nhập là bác sĩ
                DangNhapBacSi();
            }
            else
            {
                //đăng nhập là thu ngân
                DangNhapThuNgan();
            }
        }
        #endregion


        #region đăng nhập là bác sĩ
        private void DangNhapBacSi()
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select TOP 1 * from BACSI where MaBacSi = '"+ txtTenDangNhap.Text +"' AND MatKhau = '"+ MaHoaMatKhau(txtMatKhau.Text) +"'";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
            {
                lblThongBaoLoi.Text = "Đăng nhập ko thành công!.";
                return;
            }

            bacSi = new BACSI();
            bacSi.BacSi_ID = int.Parse(dt.Rows[0]["BacSi_ID"].ToString());
            bacSi.TenBacSi = dt.Rows[0]["TenBacSi"].ToString();
            bacSi.SoDienThoai = dt.Rows[0]["SoDienThoai"].ToString();
            bacSi.Khoa_ID = int.Parse(dt.Rows[0]["Khoa_ID"].ToString());
            bacSi.Email = dt.Rows[0]["Email"].ToString();
            bacSi.MaBacSi = txtTenDangNhap.Text;
            bacSi.MatKhau = MaHoaMatKhau(txtMatKhau.Text);

            //
            OnDangNhapBacSiThanhCong();
        }
        #endregion

        #region đăng nhập là thu ngân
        private void DangNhapThuNgan()
        {
            DataProvider dataProvider = new DataProvider();
            string sqlStr = "select TOP 1 * from THUNGAN where MaThuNgan = '" + txtTenDangNhap.Text + "' AND MatKhau = '" + MaHoaMatKhau(txtMatKhau.Text) + "'";
            if (!dataProvider.IsConnect())
                dataProvider.Connect();
            DataTable dt = dataProvider.ExcuteQueryDataTable(sqlStr);
            dataProvider.Disconnect();

            if (dt != null && dt.Rows.Count <= 0)
            {
                lblThongBaoLoi.Text = "Đăng nhập ko thành công!.";
                return;
            }

            thuNgan = new THUNGAN();
            thuNgan.Email = dt.Rows[0]["Email"].ToString();
            thuNgan.TenThuNgan = dt.Rows[0]["TenThuNgan"].ToString();
            thuNgan.SoDienThoai = dt.Rows[0]["SoDienThoai"].ToString();
            thuNgan.ThuNgan_ID = int.Parse(dt.Rows[0]["ThuNgan_ID"].ToString());
            thuNgan.MatKhau = MaHoaMatKhau(txtMatKhau.Text);
            thuNgan.MaThuNgan = txtTenDangNhap.Text;

            //
            OnDangNhapThuNganThanhCong();
        }
        #endregion


        #region string MaHoaMatKhau(string input)
        private string MaHoaMatKhau(string input)
        {
            // step 1, calculate MD5 hash from input
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            // step 2, convert byte array to hex string
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("x2"));
            }
            return sb.ToString();
        }
        #endregion

        #region xử lý cho btnThoat_Click()
        private void btnThoat_Click(object sender, EventArgs e)
        {
            OnDangXuat();
        }
        #endregion

        #region event DangXuat, DangXuat
        public event EventHandler DangXuat;
        private void OnDangXuat()
        {
            EventHandler handler = DangXuat;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        #endregion

        #region xử lý cho sự kiện ấn nút enter của txtMatKhau
        private void txtMatKhau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                btnDangNhap_Click(btnDangNhap, e);
            }
        }
        #endregion

        #region event dang nhap bác sĩ thanh cong, DangNhapBacSiThanhCong
        public event EventHandler DangNhapBacSiThanhCong;
        private void OnDangNhapBacSiThanhCong()
        {
            EventHandler handler = DangNhapBacSiThanhCong;
            if (handler != null) handler(bacSi, EventArgs.Empty);
        }
        #endregion

        #region event dang nhap thu ngân thanh cong, DangNhapThuNganThanhCong
        public event EventHandler DangNhapThuNganThanhCong;
        private void OnDangNhapThuNganThanhCong()
        {
            EventHandler handler = DangNhapThuNganThanhCong;
            if (handler != null) handler(thuNgan, EventArgs.Empty);
        }
        #endregion
    }
}
